import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:quality_ticket/config/appbar.dart';

class CheckBoxPageTest extends StatefulWidget {
  const CheckBoxPageTest({Key key}) : super(key: key);

  @override
  _CheckBoxPageTestState createState() => _CheckBoxPageTestState();
}

class zzs{
  String id;
  bool checkValue;

  zzs({this.id, this.checkValue});
}


class _CheckBoxPageTestState extends State<CheckBoxPageTest> {

  List<zzs> zz=[];

  bool allChecked=false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    for(var i=1;i<6;i++){
     zzs m=zzs(id:"$i",checkValue: false);
     zz.add(m);
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarPage(),
      body:
      zz.isNotEmpty?Container(
        height: MediaQuery.of(context).size.height,
        child: Column(
          children: <Widget>[
            Container(
              width: 120,
              child: CheckboxListTile(
                title: Text('老孟'),
                value: allChecked,
                onChanged: (value){
                  allChecked=value;
                  if(allChecked){
                    for(var i=0;i<zz.length;i++){
                      zz[i].checkValue=true;
                    }
                  }else{
                    for(var i=0;i<zz.length;i++){
                      zz[i].checkValue=false;
                    }
                  }
                  setState(() {
                  });
                },
              ),
            ),
           Container(
             height: MediaQuery.of(context).size.height/2,
             child:  ListView.builder(
               itemBuilder: (BuildContext context, int index) {
                 return
                   Container(
                     width: 120,
                     child: CheckboxListTile(
                       title: Text('老孟'),
                       value: zz[index].checkValue,
                       onChanged: (value){
                         zz[index].checkValue=value;
                         var mm=0;
                         for(var i=0;i<zz.length;i++){
                           if(zz[i].checkValue){
                             mm++;
                           }
                         }
                         if(mm==zz.length){
                           allChecked=true;
                         }else{
                           allChecked=false;
                         }
                         setState(() {
                         });
                       },
                     ),
                   );
               },
               itemCount: 5,
             ),
           ),
          ],
        ),
      ):Container()


    );

  }
}
